package com.piz.web.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.piz.model.User;
import com.piz.model.UserContact;
import com.piz.service.common.UnexpectedServiceException;
import com.piz.service.model.PizzaConstants;
import com.piz.service.userservice.UserService;


@Controller
@RequestMapping(value="registerUser")
public class UserController{
	
	@Autowired
	private RegistrationValidation registrationValidation;
	@Autowired
	UserService userService;
	
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView showUser(@RequestParam(value = "username",required = false) String username,User user, Model model, HttpServletRequest request){
		if(StringUtils.hasText(username))
		{
			user = userService.findUserByUsername(username);
		}
		if(user == null)
		{
			user = new User();
		}
		if(user.getContact() ==null)
		{
				user.setContact(new UserContact());
				user.setRegisterAs(PizzaConstants.RegisterAs.indv);
		}
		List<String> titles = new ArrayList<String>();
		titles.add(PizzaConstants.Title.MR);
		titles.add(PizzaConstants.Title.MRS);
		titles.add(PizzaConstants.Title.MS);
		
		List<String> regAsList = new ArrayList<String>();
		regAsList.add(PizzaConstants.RegisterAs.indv);
		regAsList.add(PizzaConstants.RegisterAs.cmp);
		
		Map<String,Object> attributes = new HashMap<String, Object>();
		attributes.put("titles", titles);
		try {
			attributes.put("states", userService.getAllStates());
		} catch (UnexpectedServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		attributes.put("regAsList", regAsList);
		
		model.addAttribute(user);
		model.addAllAttributes(attributes);
		
		Map<String, Object> refrenceData = new HashMap<String, Object>();
		return new ModelAndView("user/newUser","refrenceData",refrenceData);
	}
	
	
	@RequestMapping(method = RequestMethod.POST)
	public String processUser(@Valid  @ModelAttribute User user,BindingResult result,Map model){
		registrationValidation.validate(user, result);
		if (result.hasErrors()) {
			List<String> titles = new ArrayList<String>();
			titles.add(PizzaConstants.Title.MR);
			titles.add(PizzaConstants.Title.MRS);
			titles.add(PizzaConstants.Title.MS);
			
			List<String> regAsList = new ArrayList<String>();
			regAsList.add(PizzaConstants.RegisterAs.indv);
			regAsList.add(PizzaConstants.RegisterAs.cmp);
			
			
			model.put("titles", titles);
			try {
				model.put("states", userService.getAllStates());
			} catch (UnexpectedServiceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			model.put("regAsList", regAsList);
			model.put("user", user);
			
			return "user/newUser";
		}
		try {
			userService.createUser(user);
		} catch (UnexpectedServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.put("user", user);
		model.put("loginTxt", "You have registered successfully. Please try login here.");
		return "redirect:login.action";
	}
	
	


	public UserService getUserService() {
		return userService;
	}


	public void setUserService(UserService userService) {
		this.userService = userService;
	}


	public RegistrationValidation getRegistrationValidation() {
		return registrationValidation;
	}


	public void setRegistrationValidation(
			RegistrationValidation registrationValidation) {
		this.registrationValidation = registrationValidation;
	}


}
